define(function(require,exports,module){
    //加载bootstrap
    require("public/mgt-bootstrap212/bootstrap212");
    //$.browser.ie7 = $.browser.msie && (parseInt($.browser.version))==7;

    jQuery.each( [ "get", "post"], function( i, method ) {
        jQuery[ method ] = function( url, data, callback, type ) {
            // shift arguments if data argument was omitted
            if ( jQuery.isFunction( data ) ) {
                type = type || callback;
                callback = data;
                data = undefined;
            }
            var moduleError = function moduleError(data, textStatus, jqXHR){
               
                if(callback){
                    callback(data, textStatus, jqXHR);
                }
            }
            return jQuery.ajax({
                type: method,
                url: url,
                data: data,
                success: moduleError,
                dataType: type
            });
        };
    });

    (function($){
        $.extend({
            postJson : function(url,data,callback,trigger){
                if ( jQuery.isFunction( data ) ) {
                    trigger = trigger || callback;
                    callback = data;
                    data = undefined;
                }
                if (trigger instanceof jQuery) {
                    var prevTime = parseInt(trigger.data("mgtPostTime"))  || 0;
                    var nowTime = new Date().getTime();
                    if (Math.abs(nowTime - prevTime) < 1000) {
                        return;
                    }
                    trigger.data("mgtPostTime", nowTime);
                }       
                var moduleError = function moduleError(data, textStatus, jqXHR){
                    
                    if(callback){
                        callback(data, textStatus, jqXHR);
                    }
                };
                return $.ajax({
                    type: 'POST',
                    url: url,
                    data: JSON.stringify(data),
                    success: moduleError,
                    contentType: 'application/json'
                });
            }
        });
    })(jQuery);

    //重新登录弹框
    function showRelogin(){
        var left = ($(window).scrollLeft() + $(window).width()/2 - 175) + "px";
        var top = ($(window).scrollTop() + ($(window).height() - 150)/2) + "px";
        var html = '<div id="mgtLoginDialog" style="position:absolute;top:' + top + ';left:' + left + ';z-index:100000">'+
        '<div class="title"><b class="relogin_show_ico"></b>重新登录<a class="relogin_close" onclick="closeReloginMe();"></a></div>'+
        '<div class="popContent">'+
            '<iframe src="/mgt/login/relogin.jsp?id=' + currentUserId + '" style="width:350px;height:180px;border:0;" frameborder="0"></iframe>'+
        '</div>'+
        '</div>'+
        '';
        $(document.body).append('<div id="Relogin-hidden" class="gLoading_hidden" style="background-color:rgb(17, 17, 17)"></div>').append(html);
    }

    //关闭重新登录弹框的按钮
    function closeReloginMe(){
        $('#mgtLoginDialog').remove();
        $('#Relogin-hidden').remove();
    }
    function showLoading(){ 
        /*$(document.body).append('<div id="Loading-hidden" class="gLoading_hidden" style="height:' + $(document).height() + 'px;"></div>')
            .append('<div id="Loading-popup" class="gLoading"><img align="absmiddle" class="me-margin-right10" src="'+globalCp+'/static/v2/merged/img2/loading-web.gif"><span>' + i18n.loadingMsg + '</span></div>');
        $('#Loading-popup').css("left",($(window).scrollLeft() + $(window).width()/2- 85))
            .css("top",($(window).scrollTop() + ($(window).height() - 100)/2));
            */
    }

    function hideLoading(){
        $('#Loading-hidden').remove();
        $('#Loading-popup').remove();
    }

    $(function(){
        $(window).bind("resize",function(){
            if($('#Loading-popup')[0]){
                $('#Loading-popup').css("left",($(window).scrollLeft() + $(window).width()/2- 85))
                    .css("top",($(window).scrollTop() + ($(window).height() - 100)/2));
            }
        });
        
        $(document).ajaxStart(function(){
            showLoading();
        }).ajaxComplete(function(){
            hideLoading();
        }).ajaxError(function(event, jqXHR, ajaxSettings, thrownError){
            hideLoading();
            if(jqXHR.status == 401){
                showRelogin();
            }else if(jqXHR.status == 500){
                showModalWarn("程序出现错误，请稍后重试");
            }
        });
        
        $.ajaxSetup({
           cache:true
         });
    });
    function doAfterAppend(modal,id , msgType, destUrl){
        var p={type:msgType,id:id};
        $.post(destUrl,p,function(html){
            $(".form-horizontal").eq(0).append(html);
        });
    }
});
